package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.aliyun.ocr_api20210707.models.RecognizeBankAccountLicenseResponse;
import com.aliyun.ocr_api20210707.models.RecognizeBankAccountLicenseResponseBody;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponseBody;
import com.aliyun.tea.*;
import com.example.entity.account.Adata;
import com.example.entity.account.Opeing;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@RestController
@RequestMapping("/rz")
@CrossOrigin
public class RZController {

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.ocr_api20210707.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/ocr-api
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }
    //开户信息认证
    @PostMapping("/rz")
    public  Object rz(@RequestParam MultipartFile file) throws Exception {

        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.ocr_api20210707.Client client = RZController.createClient("LTAI5t98eNsaJyDmrxZhHyqw", "Mu6DYVxb1JfStQlc7of8F6h2i5WQuc");
        // 需要安装额外的依赖库，直接点击下载完整工程即可看到所有依赖。
        InputStream bodyStream = file.getInputStream();
        com.aliyun.ocr_api20210707.models.RecognizeBankAccountLicenseRequest recognizeBankAccountLicenseRequest = new com.aliyun.ocr_api20210707.models.RecognizeBankAccountLicenseRequest()
                .setBody(bodyStream);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        String data=null;
        try {
            // 复制代码运行请自行打印 API 的返回值
            RecognizeBankAccountLicenseResponse response = client.recognizeBankAccountLicenseWithOptions(recognizeBankAccountLicenseRequest, runtime);
            RecognizeBankAccountLicenseResponseBody body = response.getBody();
            data=body.getData();
            System.out.println(data);
            Adata adata = JSON.parseObject(data, Adata.class);

            return adata;

        } catch (TeaException error) {
            System.out.println(error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            System.out.println(error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return data;
    }

}
